package com.anyisn.anyflickr.shell.view.components.itemrender
{
	import com.adobe.webapis.flickr.Photo;
	import com.anyisn.anyflickr.common.events.PhotoLoadEvent;
	import com.anyisn.flickr.as3.utils.Urls;
	
	import flash.display.Bitmap;
	import flash.display.BitmapData;
	import flash.display.Loader;
	import flash.events.Event;
	import flash.events.IOErrorEvent;
	import flash.events.MouseEvent;
	import flash.events.ProgressEvent;
	import flash.events.TimerEvent;
	import flash.geom.Rectangle;
	import flash.net.URLRequest;
	import flash.utils.Timer;
	
	import mx.core.UIComponent;
	import mx.effects.Fade;
	
	public class ThumbIR extends UIComponent
	{
		public static const THUMB_LOADED:String = "thumbLoaded";
		public static const THUMB_LOAD_ERROR:String = "thumbLoadError";
		
		private var _bd:BitmapData;
		private var _photo:Photo;
		private var _loadingThumb:Boolean;
		private var _loadingPhoto:Boolean;
		private var _photoLoaded:Boolean;
		private var _thumbLoader:Loader;
		private var _photoLoader:Loader;
		private var _bitmap:Bitmap;
		private var _timer:Timer;
		
		public function ThumbIR(photo:Photo)
		{
			_photo = photo;
			_loadingThumb = false;
			_loadingPhoto = false;
			super();
			
			this.buttonMode = true;
			this.width = 75;
			this.height = 75;
			this.toolTip = _photo.title;
			
			loadThumb();
		}
		
		// load thumb
		private function loadThumb():void {
			_thumbLoader = new Loader();
			_thumbLoader.contentLoaderInfo.addEventListener(Event.OPEN, thumbLoadOpen);
			_thumbLoader.contentLoaderInfo.addEventListener(Event.COMPLETE, thumbLoaded);
			_thumbLoader.contentLoaderInfo.addEventListener(IOErrorEvent.IO_ERROR, thumbIOError);
			_thumbLoader.contentLoaderInfo.addEventListener(ProgressEvent.PROGRESS, thumbProgress);
			
			_loadingThumb = true;
			var url:String = Urls.getPhotoSrc(_photo, "s");
			_thumbLoader.load(new URLRequest(url));
		}
		
		private function thumbLoadOpen(e:Event):void {
			_thumbLoader.contentLoaderInfo.removeEventListener(Event.OPEN, thumbLoadOpen);
			_bd = new BitmapData(this.width, this.height, false, 0x000000);
			var bitmap:Bitmap = new Bitmap(_bd);
			addChild(bitmap);
		}
		
		private function thumbProgress(e:ProgressEvent):void {
			var rect:Rectangle = new Rectangle(0,this.height*0.8,Math.ceil((e.bytesLoaded/e.bytesTotal)*this.width),this.height*0.2);
			_bd.fillRect(rect, 0x009dff);
		}
		
		private function thumbLoaded(e:Event):void {
			_thumbLoader.contentLoaderInfo.removeEventListener(Event.COMPLETE, thumbLoaded);
			_thumbLoader.contentLoaderInfo.removeEventListener(IOErrorEvent.IO_ERROR, thumbIOError);
			_thumbLoader.contentLoaderInfo.removeEventListener(ProgressEvent.PROGRESS, thumbProgress);
			_loadingThumb = false;
			var bitmap:Bitmap  = _thumbLoader.contentLoaderInfo.content as Bitmap;
			_thumbLoader.unload();
			_thumbLoader = null;
			bitmap.addEventListener(Event.ADDED, beginFadeIn);
			
			removeChildAt(0);
			_bd = null;
			addChild(bitmap);
			dispatchEvent(new Event(THUMB_LOADED));
			addEventListener(MouseEvent.CLICK, loadPhoto);
		}
		
		// load photo
		private function loadPhoto(e:MouseEvent):void {
			removeEventListener(MouseEvent.CLICK, loadPhoto);
			_photoLoader = new Loader();
			_photoLoader.contentLoaderInfo.addEventListener(Event.OPEN, photoLoadOpen);
			_photoLoader.contentLoaderInfo.addEventListener(Event.COMPLETE, photoLoaded);
			_photoLoader.contentLoaderInfo.addEventListener(IOErrorEvent.IO_ERROR, photoIOError);
			_photoLoader.contentLoaderInfo.addEventListener(ProgressEvent.PROGRESS, photoProgress);
			
			_loadingPhoto = true;
			var url:String = Urls.getPhotoSrc(_photo);
			_photoLoader.load(new URLRequest(url));
		}
		
		private function photoLoadOpen(e:Event):void {
			_photoLoader.contentLoaderInfo.removeEventListener(Event.OPEN, photoLoadOpen);
			_bd = new BitmapData(this.width, this.height, true);
			var bitmap:Bitmap = new Bitmap(_bd);
			bitmap.alpha = 0.8;
			addChild(bitmap);
		}
		
		private function photoProgress(e:ProgressEvent):void {
			var rect:Rectangle = new Rectangle(0,0,this.width,Math.ceil((e.bytesLoaded/e.bytesTotal)*this.height));
			_bd.fillRect(rect, 0x000000);
		}
		
		private function photoLoaded(e:Event):void {
			_photoLoader.contentLoaderInfo.removeEventListener(IOErrorEvent.IO_ERROR, photoIOError);
			_photoLoader.contentLoaderInfo.removeEventListener(ProgressEvent.PROGRESS, photoProgress);
			_photoLoader.contentLoaderInfo.removeEventListener(Event.COMPLETE, photoLoaded);
			_bitmap = _photoLoader.content as Bitmap;
			_loadingPhoto = false;
			_photoLoaded = true;
			
			_photoLoader.unload();
			_photoLoader = null;
			
			removeChildAt(1);
			_bd = null;
			
			var imageL:Loader = new Loader();
			imageL.name = "yesicon";
			imageL.addEventListener(MouseEvent.RIGHT_CLICK, onRightClick);
			imageL.load(new URLRequest("/assets/images/yes_75.png"));
			imageL.alpha = 0.5;
			
			addChild(imageL);
			
			addEventListener(MouseEvent.MOUSE_OVER, onMouseOver);
		}
		
		private function onRightClick(e:MouseEvent):void {
			dispatchEvent(new MouseEvent(MouseEvent.RIGHT_CLICK, true));
		}
		
		private function onMouseOver(e:MouseEvent):void {
			addEventListener(MouseEvent.MOUSE_OUT, onMouseOut);
			_timer = new Timer(500,1);
			_timer.addEventListener(TimerEvent.TIMER_COMPLETE, onTimerCom);
			_timer.start();
//			addEventListener(MouseEvent.CLICK, closePhotoInfo);
		}
		
		private function onTimerCom(e:TimerEvent):void {
			removeEventListener(MouseEvent.MOUSE_OUT, onMouseOut);
			_timer.removeEventListener(TimerEvent.TIMER_COMPLETE, onTimerCom);
			_timer = null;
			dispatchEvent(new PhotoLoadEvent(PhotoLoadEvent.LOAD_PHOTO_SUCCESS, photo, _bitmap, true));
		}
		
		private function onMouseOut(e:MouseEvent):void {
			removeEventListener(MouseEvent.MOUSE_OUT, onMouseOut);
			if(_timer.running) {
				_timer.stop();
				_timer.removeEventListener(TimerEvent.TIMER_COMPLETE, onTimerCom);
				_timer = null;
			}
		}
		
		/*private function closePhotoInfo(e:MouseEvent):void {
			removeEventListener(MouseEvent.CLICK, closePhotoInfo);
			dispatchEvent(new Event(CLOSE_PHOTO_INFO_WIN, true));
			addEventListener(MouseEvent.MOUSE_OVER, onMouseOver);
		}*/
		
		// error handler
		private function thumbIOError(e:IOErrorEvent):void {
			_thumbLoader.contentLoaderInfo.removeEventListener(Event.COMPLETE, thumbLoaded);
			_thumbLoader.contentLoaderInfo.removeEventListener(IOErrorEvent.IO_ERROR, thumbIOError);
			_thumbLoader.contentLoaderInfo.removeEventListener(ProgressEvent.PROGRESS, thumbProgress);
			_thumbLoader.unload();
			_thumbLoader = null;
			
			_loadingThumb = false;
			dispatchEvent(new Event(THUMB_LOAD_ERROR));
		}
		
		private function photoIOError(e:IOErrorEvent):void {
			_photoLoader.contentLoaderInfo.removeEventListener(IOErrorEvent.IO_ERROR, photoIOError);
			_photoLoader.contentLoaderInfo.removeEventListener(ProgressEvent.PROGRESS, photoProgress);
			_photoLoader.contentLoaderInfo.removeEventListener(Event.COMPLETE, photoLoaded);
			_photoLoader.unload();
			_photoLoader = null;
			
			_loadingPhoto = false;
			dispatchEvent(new PhotoLoadEvent(PhotoLoadEvent.LOAD_PHOTO_FAIL, photo, null, true));
		}
		
		// fade in effect
		private function beginFadeIn(e:Event):void {
			var bitmap:Bitmap = getChildAt(0) as Bitmap;
			bitmap.removeEventListener(Event.ADDED, beginFadeIn);
			var fadeIn:Fade = new Fade(bitmap);
			fadeIn.alphaFrom = 0;
			fadeIn.alphaTo = 1;
			fadeIn.duration = 1000;
			fadeIn.play();
		}
		
		// clear before remove
		public function clear():void {
			if(hasEventListener(MouseEvent.CLICK)) {
				removeEventListener(MouseEvent.CLICK, loadPhoto);
			}
			if(hasEventListener(MouseEvent.MOUSE_OVER)) {
				removeEventListener(MouseEvent.MOUSE_OVER, onMouseOver);
			}
			if(_photoLoaded) {
				var imageL:Loader = getChildByName("yesicon") as Loader;
				imageL.removeEventListener(MouseEvent.RIGHT_CLICK, onRightClick);
			}
			while(numChildren > 0) {
				removeChildAt(0);
			}
		}
		
		public function get photo():Photo {
			return _photo;
		}
	}
}